/**
 * 缓存类
 */
export class CacheStore {
  constructor(type) {
    this.storageType = type || 'localStorage';
  }
  get(key) {
    const value = window[this.storageType].getItem(key);
    if (value || value === 0) {
      try {
        return JSON.parse(value);
      } catch (e) {
        return value;
      }
    }
    return undefined;
  }
  set(key, value) {
    const val = typeof value === 'object' ? JSON.stringify(value) : value;
    window[this.storageType].setItem(key, val);
  }
  remove(key) {
    window[this.storageType].removeItem(key);
  }
  setList(dictionaries) {
    Object.keys(dictionaries).forEach(key => {
      this.set(key, dictionaries[key]);
    });
  }
  removeList(keys) {
    keys.forEach(key => this.remove(key));
  }
}
